package com.example.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.pojo.Employee;
import com.example.server.mapper.EmployeeMapper;
import com.example.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.vo.RespBean;
import com.example.common.vo.RespPageBean;
import com.example.server.util.RestTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小红
 * @since 2022-09-02
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public RespPageBean getAll(Integer currentPage, Integer size, Employee employee) {

        Page<Employee> page = new Page<>(currentPage, size);

        IPage<Employee> result = employeeMapper.findAll(page, employee);
        return new RespPageBean()
                .setData(result.getRecords())
                .setTotal(result.getTotal());
    }

    @Override
    public RespBean maxWorkId() {
        //select max(workId) from t_empolyee
        List<Map<String, Object>> maps = employeeMapper.selectMaps(
                new QueryWrapper<Employee>().select("max(workId)"));

        String max = maps.get(0).get("max(workId)").toString();

        int maxNum = Integer.parseInt(max) + 1;

        String result = String.format("%08d", maxNum);
        return RespBean.success(result);
    }

    @Override
    public RespBean addEmp(Employee employee) {
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);

        DecimalFormat decimalFormat = new DecimalFormat("##.00");

        String format = decimalFormat.format(days / 365.00);

        employee.setContractTerm(Double.parseDouble(format));

        int insert = employeeMapper.insert(employee);

        IPage<Employee> result =
                employeeMapper.findAll(new Page<Employee>(1, 1), new Employee().setName(employee.getName()));
        Employee emp4Mail = result.getRecords().get(0);
        //新增成功的时候，id会不会自动同步过来
        //收集各个连表数据，以便邮件发送的时候使用
        RespBean respBean = RestTemplateUtils.sendPostRequest(restTemplate, emp4Mail);

        return insert == 1 ? RespBean.success() : RespBean.error(503,"新增失败");
    }

    @Override
    public List<Employee> getAllSimper() {
        return employeeMapper.findAllSimple();
    }

}
